<template>
  <div>
    <h2>商品管理</h2>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item to="/AdminMain/AdminHome">主页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card style="margin-top: 20px;">
      <template #header>
        <div class="card-header">
          <i class="el-icon-s-order" style="margin-right: 5px;"></i>商品信息表
        </div>
      </template>

      <el-table
          :data="tableData"
          style="width: 100%;"
          v-loading="loading"
          empty-text="暂无商品数据"
      >
        <el-table-column prop="gid" label="ID" />
        <el-table-column prop="goodsName" label="商品名称" />
        <el-table-column prop="goodsPrice" label="价格" />
        <el-table-column prop="goodsType" label="类型" />
        <el-table-column label="商品图片">
          <template #default="scope">
            <img
                :src="scope.row.goodsImage"
                alt="图片"
                @error="scope.row.goodsImage = '/images/default.png'"
                style="width: 80px; height: 80px; object-fit: cover; border: 1px solid #ccc;"
            />
          </template>
        </el-table-column>
        <el-table-column prop="goodsStock" label="库存" />
        <el-table-column label="操作" align="left" width="180" header-align="left">
          <template #default="scope">
            <div style="padding-left: 0px;">
              <el-button type="primary" size="small" @click="onEdit(scope.row)" style="margin-right:5px;">
                编辑
              </el-button>
              <el-button type="danger" size="small" @click="onDelete(scope.row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <div style="text-align:center;margin-top:20px;">
        <el-button type="success" @click="onAdd">添加商品</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from 'axios'

const router = useRouter()

const tableData = ref([])
const loading = ref(false)

// 封装加载商品函数
const loadData = () => {
  loading.value = true
  axios
      .get('/server/goods/search', {
        params: { page: 1, size: 10 }
      })
      .then(response => {
        if (response.data.code === 200) {
          tableData.value = response.data.data.records
        } else {
          ElMessage.error('获取数据失败: ' + (response.data.message || '未知错误'))
        }
      })
      .catch(() => {
        ElMessage.error('请求出错，无法加载数据')
      })
      .finally(() => {
        loading.value = false
      })
}

onMounted(() => {
  loadData()
})

// 编辑商品
const onEdit = (row) => {
  router.push({
    path: '/AdminMain/EditGoods',
    query: { gid: row.gid }
  })
}

// 删除商品
const onDelete = (row) => {
  ElMessageBox.confirm(
      '确定要删除商品ID为 ' + row.goodsId + ' 的商品吗？',
      '警告',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }
  ).then(() => {
    axios
        .delete('/server/goods/delete', {
          params: { goodsId: row.goodsId }
        })
        .then(response => {
          if (response.data.code === 200) {
            ElMessage.success('删除成功')
            loadData()
          } else {
            ElMessage.error('删除失败: ' + (response.data.message || '未知错误'))
          }
        })
        .catch(() => {
          ElMessage.error('请求失败')
        })
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

// 添加商品
const onAdd = () => {
  router.push('/AdminMain/AddGoods')
}
</script>

<style scoped>
.card-header {
  font-weight: bold;
}
</style>
